package com.example.administrator.zzshoping;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.RadioGroup;

import com.example.administrator.zzshoping.fragment.ClassifyFragment;
import com.example.administrator.zzshoping.fragment.HomeFragment;
import com.example.administrator.zzshoping.fragment.MessageFragment;
import com.example.administrator.zzshoping.fragment.MineFragment;
import com.example.administrator.zzshoping.fragment.ReleaseFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup mRadioGroup;
    private List<Fragment> list = new ArrayList<>();//fragment集合
    private int checkedPage = 0;//选中的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initListener();

    }

    private void initListener() {
        //mRadioGroup 选择监听
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    private void initData() {
        //fragment 初始化
        HomeFragment homeFragment = new HomeFragment();
        ClassifyFragment classifyFragment = new ClassifyFragment();
        ReleaseFragment releaseFragment = new ReleaseFragment();
        MessageFragment messageFragment = new MessageFragment();
        MineFragment mineFragment = new MineFragment();
        //添加到集合
        list.add(homeFragment);
        list.add(classifyFragment);
        list.add(releaseFragment);
        list.add(messageFragment);
        list.add(mineFragment);
        //默认展示第一个fragment
        getSupportFragmentManager().beginTransaction().add(R.id.zz_main_container, list.get(0)).commit();
    }

    private void initView() {
        mRadioGroup = ((RadioGroup) findViewById(R.id.main_rg));
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.zz_main_rgHome:
                //替换fragment(集合获取)
                checkedFragment(0);
                break;
            case R.id.zz_main_rgClassify:
                checkedFragment(1);
                break;
            case R.id.zz_main_rgRelease:
                checkedFragment(2);
                break;
            case R.id.zz_main_rgMessage:
                checkedFragment(3);
                break;
            case R.id.zz_main_rgMine:
                checkedFragment(4);
                break;
        }
    }

    private void checkedFragment(int targetPage) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = list.get(checkedPage);//当前展示
        Fragment targeFragment = list.get(targetPage);//跳转展示
            //点击按钮控制Fragment的显示和隐藏
        if (!targeFragment.isAdded()) {
            //如果没有添加到事务中  添加进来目标Fragment  隐藏当前Fragment
            transaction.add(R.id.zz_main_container, targeFragment).hide(currentFragment).commit();
        } else {
            //如果事务中存在Fragment  显示目标Fragment  隐藏当前Fragment
            transaction.show(targeFragment).hide(currentFragment).commit();
        }
        //替换当前位置
        checkedPage = targetPage;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setTitle("系统提示");
            builder.setMessage("是否退出?");
            builder.setNeutralButton("否",null);
            builder.setNegativeButton("是", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            });
            builder.show();
            return false;
        }
        return false;
    }

}
